//
//  common.swift
//  BEGlobalOiler
//
//  Created by KKING on 16/7/26.
//  Copyright © 2016年 Bullseye Energy pte ltd. All rights reserved.
//

import UIKit
import MJRefresh


//屏幕的宽和高
let SCREEN_BOUNDS = UIScreen.main.bounds
let SCREEN_SIZE = UIScreen.main.bounds.size
let SCREEN_WIDTH = UIScreen.main.bounds.width
let SCREEN_HEIGHT = UIScreen.main.bounds.height

//SCREEN_SCALE
let SCREEN_SCALE = 1/UIScreen.main.scale


//baseMargin
let baseMargin: CGFloat = 8
let smallMargin: CGFloat = 3
let buttonHeight: CGFloat = 30

/// 导航栏背景色 - 白色
let COLOR_NAV_BG = UIColor(red: 250/255.0, green: 250/255.0, blue: 250/255.0, alpha: 1)
/// 所有控制器背景颜色
let COLOR_ALL_BG = UIColor.colorWithHexString(hex: "#f5f5f8")

/// 导航栏ITEM默认 - 主题色
let COLOR_NAV_ITEM_NORMAL = UIColor.colorWithHexString(hex: "#323232")


//定义通知的名称

//push价格组成控制器
let PushPriceTotalController = "PushPriceTotalController"
//提交选择好的地理位置
let SubmitAddress = "SubmitAddress"
//评论提交按钮,更改页面img的hidden状态
let ChangeImgHidNotification = "ChangeImgHidNotification"
//注销登录,清除左侧头像和用户名的通知
let ClearSideHeaderUserInfo = "ClearSideHeaderUserInfo"
//刷新左侧头像和用户名的通知
let RefreshHeaderUserInfo = "RefreshHeaderUserInfo"




//定义通用错误信息
let AppErrorTip = "网络君正在睡觉,请稍后再试"


let sharedDF = DateFormatter()

func colorWithHex(hex:UInt32) -> UIColor{
    
    // 0xFFAA99
    let red = (hex & 0xFF0000) >> 16
    // 0x00FF00
    // => 0x00AA00 => 0x0000AA
    let green = (hex & 0x00FF00) >> 8
    // 0x1111 1111 AA 1001 1001
    // 0x0000 0000 00 1111 1111
    let blue = hex & 0x0000FF
    
    return UIColor.init(red: CGFloat(red) / 255.0, green: CGFloat(green) / 255.0, blue: CGFloat(blue) / 255.0, alpha: 1.0)
}

//baseFont
func baseFont(size:CGFloat)->UIFont{
    let font = UIFont(name: "helvetica", size: size)
    return font!
}

//GCD dispatch_after 新特性
func showNetworkActivity(){
    UIApplication.shared.isNetworkActivityIndicatorVisible = true
    //    DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 1) {
    //
    //    }
    DispatchQueue.global(qos: .background).asyncAfter(deadline: DispatchTime.now() + 1, execute: {
        UIApplication.shared.isNetworkActivityIndicatorVisible = false
    })
}

func showAlert(title: String?, message: String?, cancleTitle: String?, doneTitle: String?){
    let alertVC = UIAlertController(title: title, message: message, preferredStyle: .alert)
    let cancleAction = UIAlertAction(title: cancleTitle, style: .cancel, handler: nil)
    let doneAction = UIAlertAction(title: doneTitle, style: .default) { (_) in
    }
    alertVC.addAction(cancleAction)
    alertVC.addAction(doneAction)
    UIApplication.shared.keyWindow?.rootViewController!.present(alertVC, animated: true, completion: nil)
}

//响应者链条技术
////查找视图对象的响应者链条中的 导航视图控制器
//func navController() -> UINavigationController? {
//
//    //获取当前视图的下一个响应者
//    var next = self.nextResponder()
//    //循环遍历下一个响应者的下一个响应者
//    while next != nil {
//        if let nav = next as? UINavigationController {
//            return nav
//        }
//
//        //如果不是导航视图控制器  就继续查找
//        next = next?.nextResponder()
//    }
//    return nil
//}

/**
 手机型号枚举
 */
enum iPhoneModel {
    
    case iPhone4
    case iPhone5
    case iPhone6
    case iPhone6p
    case iPad
    
    /**
     获取当前手机型号
     
     - returns: 返回手机型号枚举
     */
    static func getCurrentModel() -> iPhoneModel {
        switch SCREEN_WIDTH {
        case 480:
            return .iPhone4
        case 568:
            return .iPhone5
        case 667:
            return .iPhone6
        case 736:
            return .iPhone6p
        default:
            return .iPad
        }
    }
}

/**
 RGB颜色构造
 */
func RGB(r: CGFloat, g: CGFloat, b: CGFloat, alpha: CGFloat) -> UIColor {
    return UIColor(red: r / 255.0, green: g/255.0, blue: b/255.0, alpha: alpha)
}

//  创建随机颜色的函数
func RandomColor() -> UIColor {
    //  产生随机的色值
    let red = arc4random() % 256
    let green = arc4random() % 256
    let blue = arc4random() % 256
    
    return UIColor(red: CGFloat(red) / 255, green: CGFloat(green) / 255, blue: CGFloat(blue) / 255, alpha: 1)
}


/**
 快速创建上拉加载更多控件
 */
func setupFooterRefresh(target: AnyObject, action: Selector) -> MJRefreshFooter {
    let footerRefresh = MJRefreshBackNormalFooter(refreshingTarget: target, refreshingAction: action)
    footerRefresh?.isAutomaticallyHidden = true
    footerRefresh?.setTitle("正在加载", for: MJRefreshState.refreshing)
    footerRefresh?.setTitle("可以松开了", for: MJRefreshState.pulling)
    footerRefresh?.setTitle("上拉加载更多", for: MJRefreshState.idle)
    footerRefresh?.setTitle("没有啦~~~", for: MJRefreshState.noMoreData)
    return footerRefresh!
}

/**
 快速创建下拉加载最新控件
 */
func setupHeaderRefresh(target: AnyObject, action: Selector) -> MJRefreshNormalHeader {
    let headerRefresh = MJRefreshNormalHeader(refreshingTarget: target, refreshingAction: action)
    headerRefresh?.lastUpdatedTimeLabel.isHidden = true
    headerRefresh?.stateLabel.isHidden = true
    return headerRefresh!
}

/**
 判断是否登录，如果没有登录则跳转到登录界面
 
 - parameter controller: 当前控制器
 
 - returns: 是否已经登录
 */
public func isLogin() -> Bool {
    
    if BEAccountModel.isLogin() {
        return true
    } else {
        let loginVc = KKNavigationViewController(rootViewController: BELoginController())
        UIApplication.shared.keyWindow?.rootViewController!.present(loginVc, animated: true, completion: {
        })
        return false
    }
}


